//
// Created by xili on 2024/8/4 21:29.
// Go big or go home.
//

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    bool isSubtree(TreeNode *root, TreeNode *subRoot) {
        if (root && subRoot) {
            return same(root, subRoot) || isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
        }
        //如果root==NULL subRoot！=NULL false
        //如果root==NULL subRoot==NULL true
        //如果root!=NULL subRoot==NULL true
        return subRoot = nullptr;
    }

    bool same(TreeNode *a, TreeNode *b) {
        if (!a && !b) {
            return true;
        }
        if (a && b) {
            return a->val == b->val && same(a->left, b->left) && same(a->right, b->right);
        }
        return false;
    }
};